package com.xy.usermanagement.authormanagement.controller;

import com.xy.model.Author;
import com.xy.model.PageList;
import com.xy.model.RestfulResult;
import com.xy.usermanagement.authormanagement.service.IAuthorManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/authormanagement")
public class AuthorManagementController {
		@Autowired
		IAuthorManagementService authorManagementService;

		@GetMapping(value = "/")
		public RestfulResult list(Integer pageNum, Integer pageSize){
				PageList<Author> list = authorManagementService.queryForPage(pageNum, pageSize);
				return RestfulResult.success(list);
		}
		@GetMapping(value = "/{name}")
		public RestfulResult getOne(@PathVariable String name) {
				if (name==null||name.equals("")){
						return RestfulResult.failed("400","查询的作者名不能为空");
				}
				Author author=null;
				try {
						author = authorManagementService.getById(name);
				}catch (Exception e){
						return RestfulResult.failed("500","服务正忙，请稍后再试！");
				}
				return RestfulResult.success(author);
		}

		@PostMapping(value = "/")
		public RestfulResult add(@ModelAttribute Author author){
				Author auth = null;
				Integer i = null;
				log.info("AuthorManagementController add=>"+author);
				try{
						if (author==null||author.getNickName().equals("")){
								return RestfulResult.failed("500","添加失败，请检查插入参数!");
						}
						auth = authorManagementService.getById(author.getNickName());
						log.info("AuthorManagementController add auth=>"+auth);
						if (auth!=null){
								return RestfulResult.failed("500","添加失败，作者以存在");
						}
						i =authorManagementService.add(author);
				}catch (Exception e){
						log.error("错误消息：{}", e.getMessage(), e);
						return RestfulResult.failed("500","添加失败，作者以存在");
				}

				return RestfulResult.success(i);
		}

		@PutMapping(value = "/{name}", produces = "application/json;charset=UTF-8")
		@Transactional
		public RestfulResult update(@PathVariable String name,@RequestBody Author author){
				log.info("AuthorManagementController update author=>"+author);
				try {
						if(name.equals("")){
								return RestfulResult.failed("500","修改的作者名不能为空！");
						}else if(authorManagementService.getById(name)==null){
								return RestfulResult.failed("500","更新的作者不存在，请输入正确的作者名！");
						}
						author.setNickName(name);
						authorManagementService.update(author);
				}catch (Exception e){
						log.error("错误消息：{}", e.getMessage(), e);
						return RestfulResult.failed("500","修改失败");
				}
				return RestfulResult.success();
		}

		@DeleteMapping(value = "/{name}")
		@Transactional
		public RestfulResult delete(@PathVariable String name){
				log.info("AuthorManagementController delete names=>"+name);
				if(name.contains(",")){
						String[] names = name.split(",");
						log.info("names=> "+names);
				}
				return RestfulResult.success();
		}
}
